/*
 * Main.java
 *
 * Created on June 4, 2006, 8:56 PM
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package hashing;
import java.util.Scanner;
/**
 *
 * @author hai
 */
public class Main {
    
    private static int[] array={5,4,23,74,98,7,43, 95, 2,49};
    private static Scanner reader= new Scanner(System.in);
    /** Creates a new instance of Main */
    public Main() {
    }
    
    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here
        int[] hTable=new int[14];
        System.out.println("Original array: ");
        for (int i=0; i<array.length; i++)
            System.out.print("  "+array[i]);
        System.out.println();
        
        hash(hTable);
        System.out.println("Hashed table: ");
        for (int i=0; i<hTable.length; i++)
            System.out.print("  "+hTable[i]);
        
        System.out.println("\n Search for: ");
        int value= reader.nextInt();
        int result= searchFor(value,hTable);
        if (result==-1)
            System.out.println(" "+ value+" is not found");
        else System.out.println(" "+ value+" is found at " +result +" in the hashed table");
        
    }
    public static int searchFor(int key, int[] hTable){
        int h1=key%13;
        int h2=key%11;
        int index=h1;
        while(hTable[index]!=key && hTable[index]!=0){
            index+=h2;
            index=index%hTable.length;
        }
        if (hTable[index]==key)
            return index;
        else return -1;
    }
    public static void hash(int[] hTable){
        for (int i=0; i<array.length; i++){
            int h1=array[i]%13;
            int h2=array[i]%11;
            
            int index= h1;
            while(hTable[index]!=0){
                index+=h2; 
                index=index%hTable.length;
                
            }
            hTable[index]=array[i];               
            
        }
    }
    
}
